package chapter15;

import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/23 11:03
 */
public class WaitThread extends Thread {
    private volatile boolean fire = false;

    public static void main(String[] args) throws InterruptedException {
        WaitThread waitThread = new WaitThread();
        waitThread.start();
        TimeUnit.SECONDS.sleep(2);
        System.out.println("fire");
        waitThread.fire();
    }

    @Override
    public void run() {
        try {
            synchronized (this) {
                while (!fire) {
                    wait();
                }
                System.out.println("fired");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void fire() {
        this.fire = true;
        notify();
    }
}
